//
//  ShoppingCarViewController.m
//  LazyCat
//
//  Created by zhanshu on 15/9/10.
//  Copyright (c) 2015年 zhanshu. All rights reserved.
//

#import "ShoppingCarViewController.h"
#import "NJKWebViewProgress.h"
#import "NJKWebViewProgressView.h"
@interface ShoppingCarViewController ()<UIWebViewDelegate,NJKWebViewProgressDelegate>
{
    NJKWebViewProgressView *_progressView;
    NJKWebViewProgress *_progressProxy;
    BOOL doLoginBack;
    __weak IBOutlet UIView *noNetWorkView;
}
@property (weak, nonatomic) IBOutlet UIWebView *webView;

@end

@implementation ShoppingCarViewController
- (void)viewWillAppear:(BOOL)animated {
    if (doLoginBack) {
        [self loadData];
        doLoginBack = NO;
    }
}
#pragma mark - 判断是否登录，没登陆去登录
- (void)refresh {
    if ([AppData share].doLogin) {
        
    }else {
        doLoginBack = YES;
//        [self goLoginView];
    }
    [self loadData];
}

- (void)viewWillDisappear:(BOOL)animated {
    NSArray *arr = self.navigationController.viewControllers;
    if (!arr) {
        _webView.delegate = nil;
        [_webView removeFromSuperview],_webView = nil;
        _progressProxy.webViewProxyDelegate = nil;
        _progressProxy.progressDelegate = nil;
        [_progressView removeFromSuperview],_progressView = nil;
    }
}
- (void)backAction:(UIButton *)btn {
    //判断是否可以HTML返回
    if (![_webView canGoBack]) {
        [self.navigationController popViewControllerAnimated:YES];
    }else {
        [_webView goBack];
    }
}
- (void)loadData {
    NSString *urlStr = [NSString stringWithFormat:ShoppingCartUrl,[AppData share].username,[AppData share].lng,[AppData share].lat,[Utils md5SignStr],[ShopCommonModel getShopMess].shopuser];
    NSURL *url = [NSURL URLWithString:urlStr];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [_webView loadRequest:request];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.view.backgroundColor = BackColor;
    [self initNav:@"购物车" doBack:NO];
    doLoginBack = NO;
    [self loadData];
    
    _progressProxy = [[NJKWebViewProgress alloc] init];
    _webView.delegate = _progressProxy;
    _progressProxy.webViewProxyDelegate = self;
    _progressProxy.progressDelegate = self;
    
    CGFloat progressBarHeight = 2.f;
    CGRect barFrame = CGRectMake(0, 64, ScreenWidth, progressBarHeight);
    _progressView = [[NJKWebViewProgressView alloc] initWithFrame:barFrame];
    [self.view addSubview:_progressView];
    
}
- (void)webViewDidStartLoad:(UIWebView *)webView {
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
    [_progressView setProgress:1 animated:NO];
    [self cleanMemory];
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
    if (kNetworkNotReachability) {
        [[MessShowView share] showWithMess:@"网络不给力"];
        noNetWorkView.hidden = NO;
        _webView.hidden = YES;
        [_progressView setProgress:1 animated:NO];
    }
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    BOOL doAlloc = [JsWebviewNativeManager theRequestUrl:[request.URL absoluteString] viewController:self success:^(JsWithWB type, NSString *count) {
        if (type == SendNeedLogin) {
            doLoginBack = YES;
            [_webView goBack];
        }
    }];
    return doAlloc;
}
#pragma mark - NJKWebViewProgressDelegate
-(void)webViewProgress:(NJKWebViewProgress *)webViewProgress updateProgress:(float)progress
{
    [_progressView setProgress:progress animated:YES];
    //    self.title = [_webView stringByEvaluatingJavaScriptFromString:@"document.title"];
}
#pragma mark - 清除内存
- (void)cleanMemory {
    [[NSUserDefaults standardUserDefaults] setInteger:0 forKey:@"WebKitCacheModelPreferenceKey"];
    [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"WebKitDiskImageCacheEnabled"];//自己添加的，原文没有提到。
    [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"WebKitOfflineWebApplicationCacheEnabled"];//自己添加的，原文没有提到。
    [[NSUserDefaults standardUserDefaults] synchronize];
}
@end
